﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Crosland.Automotriz.Logic.Linq;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxPivotGrid;
using System.Data;
using DevExpress.XtraPivotGrid.Data;


namespace Crosland.Automotriz.Web.Vehiculos
{
    public partial class wFrmStockDealerIntranet : System.Web.UI.Page
    {
        string idNegocio = ConfigurationManager.AppSettings["Negocio"];
        const string script = "MostrarAlerta();";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                Panel img = (Panel)Master.FindControl("asp");
                img.Visible = false;
                ListaAnio();
                ListarMarcas();
                cboMes.Value = DateTime.Now.Month.ToString();
            }
            
            ListarCuadro1();
            ListarCuadro2();
            pv.CellTemplate = new CellTemplate();
        }

        void ListaAnio()
        {
            clsTH_VentaBL da = new clsTH_VentaBL();
            IEnumerable<object> años;
            años = da.AniosFactura();
            cboAño.DataSource = años;
            cboAño.TextField = "anio";
            cboAño.ValueField = "anio";
            cboAño.DataBind();
            cboAño.Value = DateTime.Now.Year.ToString();

            cboAño0.DataSource = años;
            cboAño0.TextField = "anio";
            cboAño0.ValueField = "anio";
            cboAño0.DataBind();
            cboAño0.Value = DateTime.Now.Year.ToString();
        }

        void ListarMarcas()
        {
            clsTD_MarcaBL neg = new clsTD_MarcaBL();
            List<Entidad.TD_MARCA> ent = new List<Entidad.TD_MARCA>();
            ent = neg.ListarMarcas();
            cboMarca.DataSource = ent;
            cboMarca.TextField = "DES_MARCA";
            cboMarca.ValueField = "ID_MARCA";
            cboMarca.DataBind();
            cboMarca.SelectedIndex = 0;

            cboMarca0.DataSource = ent;
            cboMarca0.TextField = "DES_MARCA";
            cboMarca0.ValueField = "ID_MARCA";
            cboMarca0.DataBind();
            cboMarca0.SelectedIndex = 0;
        }

        void ListarCuadro1() 
        {
            int anio = int.Parse(cboAño.Value.ToString());
            int mes = int.Parse(cboMes.Value.ToString());
            int marca = int.Parse(cboMarca.Value.ToString());
            clsT_Ven_Sell_In_Out_StockBL neg = new clsT_Ven_Sell_In_Out_StockBL();
            pv.DataSource = neg.ListarIntranet(anio, mes, marca);
            pv.DataBind();
        }

        void ListarCuadro2() 
        {
            int anio = int.Parse(cboAño0.Value.ToString());
            int marca = int.Parse(cboMarca0.Value.ToString());
            clsT_Ven_Sell_In_Out_StockBL neg = new clsT_Ven_Sell_In_Out_StockBL();
            pv0.DataSource = neg.ListarIntranetAnual(anio, marca);
            pv0.DataBind();
        }

        protected void btnSalir_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Repuestos/wFrmDefault.aspx?id=" + idNegocio);
        }

        protected void btnConsultar_Click(object sender, EventArgs e)
        {
            ListarCuadro1();
            ListarCuadro2();
        }

        class CellTemplate : ITemplate
        {
            public void InstantiateIn(Control container)
            {
                PivotGridCellTemplateContainer c = container as PivotGridCellTemplateContainer;
                List<object> fieldValues = new List<object>();
                if (c.ColumnField != null)
                    fieldValues.Add(c.GetFieldValue(c.ColumnField));
                if (c.RowField != null)
                    fieldValues.Add(c.GetFieldValue(c.RowField));

                c.Controls.Add(new MyLink(c.Text, fieldValues));
            }
        }

        public class MyLink : HyperLink
        {
            public MyLink(string text, List<object> values)
                : base()
            {
                Text = text;
                NavigateUrl = "#";

                string supervisor="";
                string anio = "";
                string mes = "";
                string marca = "";
                string[] cadena;
                int x = 0;
                foreach (object value in values)
                {
                    if (value != null && x == 1)
                    {
                        cadena = value.ToString().Split(char.Parse("|"));
                        if (cadena.Length == 4)
                        {
                            anio = cadena[0].ToString();
                            mes = int.Parse(cadena[1].ToString()).ToString();
                            marca = cadena[2].ToString();
                            supervisor = cadena[3].ToString();
                        }
                    }
                    x++;
                }
                Attributes["onclick"] = "window.open('wFrmStockDealerIntranetSupervisor.aspx?sup=" + supervisor + "&anio=" + anio + "&mes=" + mes + "&marca=" + marca + "&id=3','_self')";
            }
        }

    }
}